<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="孵化批次编号" prop="hatchBatchNo">
        <el-input
          v-model="queryParams.hatchBatchNo"
          placeholder="请输入孵化批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孵化开始时间" prop="hatchStartTime">
        <el-date-picker clearable
          v-model="queryParams.hatchStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择孵化开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="孵化结束时间" prop="hatchEndTime">
        <el-date-picker clearable
          v-model="queryParams.hatchEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择孵化结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['btlfbreed:chickenHatchRecords:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['btlfbreed:chickenHatchRecords:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['btlfbreed:chickenHatchRecords:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['btlfbreed:chickenHatchRecords:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chickenHatchRecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="孵化批次编号" align="center" prop="hatchBatchNo" />
      <el-table-column label="孵化开始时间" align="center" prop="hatchStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hatchStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="孵化结束时间" align="center" prop="hatchEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hatchEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="孵化数量" align="center" prop="totalEggs" />
      <el-table-column label="孵化成功数量" align="center" prop="hatchedChicks" />
      <el-table-column label="死亡数量" align="center" prop="deadEggs" />
      <el-table-column label="孵化率" align="center" prop="hatchRate" />
      <el-table-column label="死亡率" align="center" prop="mortalityRate" />
      <el-table-column label="育种员" align="center" prop="userName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['btlfbreed:chickenHatchRecords:edit']"
          >肉鸡登记</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['btlfbreed:chickenHatchRecords:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改肉鸡孵化记录对话框 -->
    <el-dialog :title="title" fullscreen="true" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="孵化批次编号" prop="hatchBatchNo">
          <el-input v-model="form.hatchBatchNo" placeholder="请输入孵化批次编号" />
        </el-form-item>
        <el-form-item label="孵化开始时间" prop="hatchStartTime">
          <el-date-picker clearable
            v-model="form.hatchStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择孵化开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="孵化结束时间" prop="hatchEndTime">
          <el-date-picker clearable
            v-model="form.hatchEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择孵化结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="孵化数量" prop="totalEggs">
          <el-input v-model="form.totalEggs" placeholder="请输入孵化数量" />
        </el-form-item>
        <el-form-item label="孵化成功数量" prop="hatchedChicks">
          <el-input v-model="form.hatchedChicks" placeholder="请输入孵化成功数量" />
        </el-form-item>
        <el-form-item label="死亡数量" prop="deadEggs">
          <el-input v-model="form.deadEggs" placeholder="请输入死亡数量" />
        </el-form-item>
        <!-- <el-form-item label="孵化率" prop="hatchRate">
          <el-input v-model="form.hatchRate" placeholder="请输入孵化率" />
        </el-form-item>
        <el-form-item label="死亡率" prop="mortalityRate">
          <el-input v-model="form.mortalityRate" placeholder="请输入死亡率" />
        </el-form-item> -->
        <el-form-item label="育种员" prop="userId">
          <el-select v-model="form.userId"  placeholder="请选择育种员">
                <el-option
                  v-for="item in userOptions"
                  :key="item.userId"
                  :label="item.userName"
                  :value="item.userId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <div v-show="isShow">
        <el-divider content-position="center">肉鸡登记信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbChickens">登记</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbChickens">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbChickensList" :row-class-name="rowTbChickensIndex" @selection-change="handleTbChickensSelectionChange" ref="tbChickens">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="所属鸡舍" prop="houseId" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.houseId" placeholder="请选择所属鸡舍">
                <el-option
                  v-for="item in houseOptions"
                  :key="item.houseId"
                  :label="item.houseName"
                  :value="item.houseId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="孵化批次编号" prop="hatchBatchNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.hatchBatchNo" placeholder="请输入孵化批次编号" />
            </template>
          </el-table-column>
          <el-table-column label="肉鸡编号" prop="chickenNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.chickenNo" placeholder="请输入肉鸡编号" />
            </template>
          </el-table-column>
          <el-table-column label="出生日期" prop="birthDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.birthDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出生日期" />
            </template>
          </el-table-column>
          <el-table-column label="当前体重" prop="weight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" placeholder="请输入当前体重" />
            </template>
          </el-table-column>
          <el-table-column label="出栏日期" prop="jhclDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.jhclDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择出栏日期" />
            </template>
          </el-table-column>
          <el-table-column label="健康状况" prop="healthStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.healthStatus" placeholder="请选择健康状况">
                <el-option
                  v-for="dict in dict.type.tb_chickens_health_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保  存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChickenHatchRecords, getChickenHatchRecords, delChickenHatchRecords, addChickenHatchRecords, updateChickenHatchRecords,getOptionselect } from "@/api/btlfbreed/chickenHatchRecords";

export default {
  name: "ChickenHatchRecords",
  dicts: ['tb_chickens_health_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbChickens: [],
      // 用户选项
      userOptions: [],
      // 用户选项
      houseOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 肉鸡孵化记录表格数据
      chickenHatchRecordsList: [],
      // 肉鸡表格数据
      tbChickensList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isShow:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hatchBatchNo: null,
        hatchStartTime: null,
        hatchEndTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询肉鸡孵化记录列表 */
    getList() {
      this.loading = true;
      listChickenHatchRecords(this.queryParams).then(response => {
        this.chickenHatchRecordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hatchBatchNo: null,
        hatchStartTime: null,
        hatchEndTime: null,
        totalEggs: null,
        hatchedChicks: null,
        deadEggs: null,
        hatchRate: null,
        mortalityRate: null,
        userId: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.tbChickensList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getOptionselect().then(response=>{
        this.userOptions=response.data.users;
        this.houseOptions=response.data.chickenHouses;

      });
      this.reset();
      this.open = true;
      this.isShow=false;
      this.title = "添加肉鸡孵化记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getOptionselect().then(response=>{
        this.userOptions=response.data.users;
        this.houseOptions=response.data.chickenHouses;

      });
      this.reset();
      const id = row.id || this.ids
      getChickenHatchRecords(id).then(response => {
        this.form = response.data;
        this.tbChickensList = response.data.tbChickensList;
        this.open = true;
        this.isShow= true;
        this.title = "修改肉鸡孵化记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbChickensList = this.tbChickensList;
          if (this.form.id != null) {
            updateChickenHatchRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChickenHatchRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除肉鸡孵化记录编号为"' + ids + '"的数据项？').then(function() {
        return delChickenHatchRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 肉鸡序号 */
    rowTbChickensIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 肉鸡添加按钮操作 */
    handleAddTbChickens() {
      let obj = {};
      obj.houseId = "";
      obj.hatchBatchNo =  this.form.hatchBatchNo;
      obj.chickenNo = "";
      obj.birthDate = "";
      obj.weight = "";
      obj.jhclDate = "";
      obj.healthStatus = "";
      obj.remark = "";
      this.tbChickensList.push(obj);
    },
    /** 肉鸡删除按钮操作 */
    handleDeleteTbChickens() {
      if (this.checkedTbChickens.length == 0) {
        this.$modal.msgError("请先选择要删除的肉鸡数据");
      } else {
        const tbChickensList = this.tbChickensList;
        const checkedTbChickens = this.checkedTbChickens;
        this.tbChickensList = tbChickensList.filter(function(item) {
          return checkedTbChickens.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbChickensSelectionChange(selection) {
      this.checkedTbChickens = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('btlfbreed/chickenHatchRecords/export', {
        ...this.queryParams
      }, `chickenHatchRecords_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
